package com.sf.proxy.custom.dynamic.jdkcustom;


import com.sf.proxy.custom.dynamic.jdkcustom.proxy.SFClassLoader;
import com.sf.proxy.custom.dynamic.jdkcustom.proxy.SFInvocationHandler;
import com.sf.proxy.custom.dynamic.jdkcustom.proxy.SFProxy;

import java.lang.reflect.Method;

/**
 *  基于 JDK 实现
 *
 *  这样黄牛就不知可以 给Me 买票了，所有人的票 它都可以帮着买
 */
public class HuangNiuProxy implements SFInvocationHandler {

    private Person person;

    public void setObject(Person person) {
        this.person = person;
    }

    public Object getProxy(){
        return SFProxy.newProxyInstance(new SFClassLoader(),person.getClass().getInterfaces(),this);
    }


    private void before(){
        System.out.println("黄牛:买票前,先收钱");
    }
    private void after(){
        System.out.println("黄牛:买票后,通知用户买好了");
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object invoke = method.invoke(person, args);
        after();
        return invoke;
    }
}
